﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Threading;

    internal class ContextViewModel : INotifyPropertyChanged
    {
        private BusinessLogicModel _businessLogicModel;
        private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData _contextData;
        private List<EntityViewModel> _entities;

        public event PropertyChangedEventHandler PropertyChanged;

        public ContextViewModel(BusinessLogicModel businessLogicModel, Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData contextData)
        {
            this._businessLogicModel = businessLogicModel;
            this._contextData = contextData;
        }

        internal void EntityStateChanged()
        {
            this.RaisePropertyChanged("IsMetadataClassGenerationAllowed");
            this.RaisePropertyChanged("IsMetadataClassGenerationRequested");
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public override string ToString()
        {
            return this.Name;
        }

        public Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData ContextData
        {
            get
            {
                return this._contextData;
            }
        }

        public IEnumerable<EntityViewModel> Entities
        {
            get
            {
                if (this._entities == null)
                {
                    EntityData[] entityDataItemsForContext = this._businessLogicModel.GetEntityDataItemsForContext(this.ContextData);
                    this._entities = new List<EntityViewModel>();
                    foreach (EntityData data in entityDataItemsForContext)
                    {
                        EntityViewModel item = new EntityViewModel(this, data);
                        this._entities.Add(item);
                    }
                    this._entities.Sort((Comparison<EntityViewModel>) ((x, y) => string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase)));
                }
                return this._entities;
            }
        }

        public bool IsClientAccessEnabled
        {
            get
            {
                return this.ContextData.IsClientAccessEnabled;
            }
            set
            {
                if (value != this.ContextData.IsClientAccessEnabled)
                {
                    this.ContextData.IsClientAccessEnabled = value;
                    this.RaisePropertyChanged("IsClientAccessEnabled");
                }
            }
        }

        public bool IsODataEndpointEnabled
        {
            get
            {
                return this.ContextData.IsODataEndpointEnabled;
            }
            set
            {
                if (value != this.ContextData.IsODataEndpointEnabled)
                {
                    this.ContextData.IsODataEndpointEnabled = value;
                    this.RaisePropertyChanged("IsODataEndpointEnabled");
                }
            }
        }

        public string Name
        {
            get
            {
                return this.ContextData.Name;
            }
        }
    }
}

